package com.witmore.neutron.ai.web.apiv2.nls;//package com.witmore.neutron.ai.web.tts;

import com.alibaba.nls.client.AccessToken;
import com.witmore.neutron.ai.web.apiv2.nls.config.NlsProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;


@Slf4j
@Component
public class NlsTokenClient {


    @Autowired
    private NlsProperties nlsProperties;

    /**
     * 获取 AccessToken
     */
    public AccessToken getAccessToken() {
        AccessToken accessToken = new AccessToken(nlsProperties.getAccessKeyId(), nlsProperties.getAccessKeySecret());
        try {
            accessToken.apply();
            log.info("Token: {}, expire time: {}", accessToken.getToken(), accessToken.getExpireTime());
            return accessToken;
        } catch (IOException e) {
            log.error("Failed to get access token", e);
            throw new RuntimeException("Failed to get access token", e);
        }
    }

}
